<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

/**
 * Class Authenticate
 * @package App\Http\Middleware
 */
class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if(env("MOCK_LOGIN")) {
                \Auth::loginUsingId(\App\Models\Access\User\User::SUPER_ADMIN);
            } else {
                \Log::info("Authenticate user is not login.......");
                if ($request->ajax() || $request->wantsJson()) {
                    return response('Unauthorized.', 401);
                } else {
                    return redirect()->guest(env('CAS_SERVICE_URI'));
                }
            }

        }
        return $next($request);
    }
}